package com.neusoft;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class LoginServlet02
 */
@WebServlet("/LoginServlet02")
public class LoginServlet02 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet02() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//开启cookie后不能使用*，这里使用resquest.getHeader("Origin")获取前端域【请求头】
		response.setHeader("Access-Control-Allow-Origin",request.getHeader("origin") );
		//设置开启cookie
		response.setHeader("Access-Control-Allow-Credentials","true");
		
		 //向Request作用域中添加属性
        request.setAttribute("message", "Request信息");
        //向Session作用域中添加属性
        request.getSession().setAttribute("message", "Session信息");
        //向ServletContext作用域中添加属性
        request.getServletContext().setAttribute("message", "ServletContext信息");
		
		
		
		/*PrintWriter out = response.getWriter();
		HttpSession session = request.getSession();
		System.out.println(session.getId());
		
		String userName = request.getParameter("userName");
		String password = request.getParameter("password");	
		
		if(userName.equals("test") && password.equals("111")){
			session.setAttribute("user", "userInfor");
			out.print("ok");*/
			
			/*
			System.out.println("登录OK");
			//创建用户信息放在cookie里
			Cookie userCookie = new Cookie("user","ok");
			//设定cookie的过期时间
			userCookie.setMaxAge(365 *24 * 60 * 60);
			//向客户端发送cookie
			response.addCookie(userCookie);
			*/
		/*	
		}else{
			out.print("error");
		}
		out.close();
		*/
		}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
